﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace POI.eTransmittal.BusinessRule
{
    static class ADQuery
    {
        public static string[] QueryEmployeeData(string url)
        {
            using (ADSIQuery adQuery = new ADSIQuery(url))
            {
                string[] queryResult = null;
                try
                {
                    queryResult = adQuery.QueryExtended("(&(objectCategory=person)(objectClass=User))", new string[] { "mail", "title", "cn" });
                    //queryResult = adQuery.QueryExtended("(objectCategory=user)", new string[] { "mail", "title", "cn" });

                }
                catch (System.Net.WebException e)
                {
                    throw new Exception("Unable to Query Employee Data");
                }

                return queryResult;
            }
        }

        private static DataTable CreateEmptyEmployeeTbl()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn("mail", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Title", typeof(string)));
            dt.Columns.Add(new DataColumn("Email", typeof(string)));
            return dt;
        }

        public static DataTable GetEmployeeDataTable(IEnumerable<string> employeeQueryResult)
        {
            if (employeeQueryResult == null) return null;

            DataTable employeeTbl = CreateEmptyEmployeeTbl();

            foreach (string row in employeeQueryResult)
            {
                DataRow dr = employeeTbl.NewRow();

                string[] fields = row.Split(';');
                Hashtable hashField = new Hashtable();
                foreach (string field in fields)
                {
                    if (string.IsNullOrEmpty(field)) continue;
                    string[] pair = field.Split('=');
                    if (pair.Length > 0) { hashField.Add(pair[0], pair[1]); }
                }

                if (hashField.ContainsKey("mail")) { dr["mail"] = hashField["mail"]; }
                if (hashField.ContainsKey("title")) { dr["Title"] = hashField["title"]; }
                if (hashField.ContainsKey("cn")) { dr["Name"] = hashField["cn"]; }

                employeeTbl.Rows.Add(dr);
            }

            if (employeeTbl == null) return null;

            DataRow[] rows = employeeTbl.Select("mail <> '' AND mail NOT LIKE 'SystemMailBox*'");

            DataTable filteredTable = employeeTbl.Clone();
            foreach (DataRow row in rows)
            {
                row["Email"] = row["mail"].ToString().ToLower();
                row["mail"] = row["mail"].ToString().ToLower().Replace("@premier-oil.com", "");
                row.AcceptChanges();
                filteredTable.ImportRow(row);
            }

            return filteredTable;
        }
    }
}
